package com.healthcloud.service.hospital.impl;

import com.healthcloud.dao.hospital.SysUserOrganiDao;
import com.healthcloud.entity.hospital.SysUserOrganiEntity;
import com.healthcloud.service.hospital.SysUserOrganiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service("sysUserOrganiService")
@Transactional
public class SysUserOrganiServiceImpl implements SysUserOrganiService {

	@Autowired
	private SysUserOrganiDao sysUserOrganiDao;

	@Override
	public void add(Long userId, Long organiId) {
		SysUserOrganiEntity entity = queryByUserId(userId);

		//没有就新建,有则修改
		if (entity!=null) {
			if (organiId==null){
				sysUserOrganiDao.delete(entity.getId());
				return;
			}
			if (entity.getOrganiId()==organiId){
				return;
			}
			entity.setOrganiId(organiId);
			entity.setUpdateTime(new Date());
			sysUserOrganiDao.update(entity);
		}else {
			entity = new SysUserOrganiEntity();
			entity.setOrganiId(organiId);
			entity.setUserId(userId);
			entity.setCreateTime(new Date());
			entity.setUpdateTime(new Date());
			sysUserOrganiDao.save(entity);
		}
	}

	@Override
	public SysUserOrganiEntity queryByUserId(Long userId) {
		return sysUserOrganiDao.queryByUserId(userId);
	}
}
